package com.yuyou.fn.updater.handler;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.wens.mybatisplus.examples.Example;
import com.yuyou.fn.balance.entity.TmsBalanceTransfer;
import com.yuyou.fn.balance.service.ITmsBalanceTransferService;
import com.yuyou.fn.common.database.event.EventHandler;
import com.yuyou.fn.common.database.event.IEventHandler;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.crm.entity.CrmCommunicateHistory;
import com.yuyou.fn.crm.entity.CrmStudentCommunicate;
import com.yuyou.fn.crm.service.ICrmCommunicateHistoryService;
import com.yuyou.fn.crm.service.ICrmStudentCommunicateService;
import com.yuyou.fn.educational.entity.AttendClassRecord;
import com.yuyou.fn.educational.entity.Student;
import com.yuyou.fn.educational.entity.StudentRefund;
import com.yuyou.fn.educational.entity.StudentReg;
import com.yuyou.fn.educational.service.IAttendClassRecordService;
import com.yuyou.fn.educational.service.IStudentRefundService;
import com.yuyou.fn.educational.service.IStudentRegService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Set;

@EventHandler(tables = {"yyfn.student"})
@Component
public class StudentChangeUpdater implements IEventHandler {

    @Resource
    private IStudentRegService studentRegService;

    @Resource
    private IAttendClassRecordService attendClassRecordService;

    @Resource
    private IStudentRefundService studentRefundService;



    @Resource
    private ICrmCommunicateHistoryService crmCommunicateHistoryService;

    @Resource
    private ICrmStudentCommunicateService crmStudentCommunicateService;

    @Resource
    private ITmsBalanceTransferService tmsBalanceTransferService;

    @Override
    public void insert(String tableName, JSONArray rowList) {

    }

    @Override
    public void update(String tableName, JSONArray rowList, Set<String> updateColumns) {


        if (updateColumns.contains("name")) {
            for (int i = 0; i < rowList.size(); i++) {
                JSONObject row = rowList.getJSONObject(i);
                Long studentId = row.getLong("student_id");
                String name = row.getString("name");
                Example<StudentReg> studentRegExample = StudentReg.createExample();
                studentRegExample.createCriteria().andEqualTo("studentId", studentId);

                StudentReg updateStudent = new StudentReg();
                updateStudent.setStudentName(name);
                updateStudent.setUpdateTime(TimeUtils.now());
                studentRegService.updateSelectiveByExample(updateStudent, studentRegExample);

                Example<AttendClassRecord> attendClassRecordExample = AttendClassRecord.createExample();
                attendClassRecordExample.createCriteria().andEqualTo("studentId", studentId );

                AttendClassRecord updateAttendClassRecord = new AttendClassRecord();
                updateAttendClassRecord.setStudentName(name);
                attendClassRecordService.updateSelectiveByExample(updateAttendClassRecord,attendClassRecordExample);

                Example<StudentRefund> studentRefundExample = StudentRefund.createExample();
                studentRefundExample.createCriteria().andEqualTo("studentId", studentId );

                StudentRefund  updateStudentRefund = new StudentRefund();
                updateStudentRefund.setStudentName(name);
                studentRefundService.updateSelectiveByExample(updateStudentRefund,studentRefundExample);




                Example<TmsBalanceTransfer> tmsBalanceTransferExample=TmsBalanceTransfer.createExample();
                tmsBalanceTransferExample.createCriteria().andEqualTo("studentId",studentId);
                TmsBalanceTransfer tmsBalanceTransfer=new TmsBalanceTransfer();
                tmsBalanceTransfer.setStudentName(name);
                tmsBalanceTransferService.updateSelectiveByExample(tmsBalanceTransfer,tmsBalanceTransferExample);


                Example<CrmCommunicateHistory> crmCommunicateHistoryExample=CrmCommunicateHistory.createExample();
                crmCommunicateHistoryExample.createCriteria().andEqualTo("studentId",studentId);
                CrmCommunicateHistory crmCommunicateHistory=new CrmCommunicateHistory();
                crmCommunicateHistory.setStudentName(name);
                crmCommunicateHistoryService.updateSelectiveByExample(crmCommunicateHistory,crmCommunicateHistoryExample);

                Example<CrmStudentCommunicate> crmStudentCommunicateExample=CrmStudentCommunicate.createExample();
                crmStudentCommunicateExample.createCriteria().andEqualTo("studentId",studentId);
                CrmStudentCommunicate crmStudentCommunicate=new CrmStudentCommunicate();
                crmStudentCommunicate.setStudentName(name);
                crmStudentCommunicateService.updateSelectiveByExample(crmStudentCommunicate,crmStudentCommunicateExample);

            }
            if (updateColumns.contains("phoneNo")){
                for (int i = 0; i < rowList.size(); i++) {
                    JSONObject row = rowList.getJSONObject(i);
                    Long studentId = row.getLong("student_id");
                    String phoneNo = row.getString("phoneNo");
                    Example<TmsBalanceTransfer> tmsBalanceTransferExample=TmsBalanceTransfer.createExample();
                    tmsBalanceTransferExample.createCriteria().andEqualTo("studentId",studentId);
                    TmsBalanceTransfer tmsBalanceTransfer=new TmsBalanceTransfer();
                    tmsBalanceTransfer.setStudentPhoneNo(phoneNo);
                    tmsBalanceTransferService.updateSelectiveByExample(tmsBalanceTransfer,tmsBalanceTransferExample);

                    Example<StudentReg> studentRegExample = StudentReg.createExample();
                    studentRegExample.createCriteria().andEqualTo("studentId", studentId );
                    StudentReg studentReg = new StudentReg();
                    studentReg.setUpdateTime(TimeUtils.now());
                    studentRegService.updateSelectiveByExample(studentReg,studentRegExample);



                    Example<CrmCommunicateHistory> crmCommunicateHistoryExample=CrmCommunicateHistory.createExample();
                    crmCommunicateHistoryExample.createCriteria().andEqualTo("studentId",studentId);
                    CrmCommunicateHistory crmCommunicateHistory=new CrmCommunicateHistory();
                    crmCommunicateHistory.setStudentPhoneNo(phoneNo);
                    crmCommunicateHistoryService.updateSelectiveByExample(crmCommunicateHistory,crmCommunicateHistoryExample);

                    Example<CrmStudentCommunicate> crmStudentCommunicateExample=CrmStudentCommunicate.createExample();
                    crmStudentCommunicateExample.createCriteria().andEqualTo("studentId",studentId);
                    CrmStudentCommunicate crmStudentCommunicate=new CrmStudentCommunicate();
                    crmStudentCommunicate.setStudentPhoneNo(phoneNo);
                    crmStudentCommunicateService.updateSelectiveByExample(crmStudentCommunicate,crmStudentCommunicateExample);

                }
            }
        }
    }

    @Override
    public void delete(String tableName, JSONArray rowList) {
    }
}
